home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12126 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: christoph.ol.sub.de!news
  2. From: andi@christoph.ol.sub.de (Andreas Christoph)
  3. Newsgroups: gnu.g++.help,comp.lang.c++
  4. Subject: Re: Why not?  c++ Array of strings...
  5. Date: 18 Mar 1996 10:39:00 GMT
  6. Organization: private
  7. Message-ID: <4ijeg4$7k8@christoph.ol.sub.de>
  8. References: <4ii3pp$4df@panix.com>
  9. NNTP-Posting-Host: christoph.ol.sub.de
  10. X-Newsreader: knews 0.9.3
  11. In-Reply-To: <4ii3pp$4df@panix.com>
  12. To: acinader@panix.com (Arthur Cinader Jr)
  13.  
  14. In article <4ii3pp$4df@panix.com>,
  15.     acinader@panix.com (Arthur Cinader Jr) writes:
  16. >Note Followup
  17. >
  18. >I want a class to a have a member that is an array of
  19. >character strings that are dynamically allocated.  I have written 
  20. >a very simple program that shows the eveolution of my thought
  21. >that leads me to beleieve that I should be able to do what I
  22. >am trying, but I get a segmentation fault and core dump when I
  23. >try it.  Why?  I obviously am missing something.  I have kept
  24. >the code as brief as possible (I'm using g++ v2.4):
  25. >
  26. >
  27. >***foo.h
  28. >class Foo {
  29. >    public:
  30. >        Foo();      // constructor
  31. >        ~Foo();     // destructor
  32. >    private:
  33. >        char a[10];  // declare and define an array of characters
  34. >        char *b;     // declare a string pointer
  35. >        char *c[10]; // declare an array of ten strings?
  36. >};
  37. >
  38. >***foo.C
  39. >nclude <iostream.h>
  40. >#include <assert.h>
  41. >#include <string.h>
  42. >#include "foo.h"
  43. >
  44. >// Constructor -- all the action is here --
  45. >Foo::Foo()
  46. >{
  47. >        // populate the character array
  48. >        a[0] = 'a';
  49. >        a[1] = 'r';
  50. >        a[2] = 't';
  51. >        a[3] = 'h';
  52. >        a[4] = 'u';
  53. >        a[5] = 'r';
  54. >
  55. >        // print the character array
  56. >        cout << "output from array of char: ";
  57. >        for(int i = 0 ; i < 6 ; i++)
  58. >                cout << a[i];
  59. >
  60. >        cout << endl;
  61. >
  62. >        // populate the string
  63. >        b = new char[7];                // allocate space
  64. >        assert( b != 0);                // make sure space was allocated
  65. >        strcpy(b, "arthur");
  66. >
  67. >        // print the string
  68. >        cout << "output from *char: " << b;
  69. >
  70. >        cout << endl;
  71. >
  72. >        // So far so good.  This is where it all falls apart
  73. >        // populate array of strings
  74. >        c[0] = new char[10];    // allocate space
  75. >        assert(c[0] != 0);      // make sure space was allocated
  76. >        strcpy(c[0], "arthur");
  77. >        c[1] = new char[10];
  78. >        assert(c[1] != 0);
  79. >        strcpy(c[1], "cinader");
  80. >        c[2] = new char[10];
  81. >        assert(c[2] != 0);
  82. >        strcpy(c[2], "jr.");
  83. >
  84. >        // output array of strings
  85. >        for (i = 0 ; i < 3 ; i--)
  86.                               **** 
  87. This should be i++, of course.
  88.  
  89.                                
  90. >                cout << c[i] << " ";
  91. >
  92. >        cout << endl;
  93. >
  94. >}
  95. >
  96. >Foo::~Foo() {
  97. >        // clean up the mess
  98. >        delete [] b;
  99. >        for(int i = 0; i < 3; i --)
  100.                                *****
  101. same here, i++
  102.  
  103. >                delete [] c[i];
  104.  
  105.  
  106. >}
  107. >
  108. >
  109. >**** foo.driver.C
  110. >#include "foo.h"
  111. >
  112. >main()
  113. >{
  114. >        Foo f;
  115. >
  116. >        return 0;
  117. >}
  118.  
  119. With these two changes it compiled and ran fine with GCC 2.7.0
  120. under my Linux 1.2.13
  121.  
  122. Bye,
  123. Andreas.
  124.  
  125.  
  126. -- 
  127. --------------------------------------------------------------------
  128. Andreas Christoph
  129. Alexanderstr. 292
  130. 26127 Oldenburg
  131. --------------------------------------------------------------------
  132.  
  133.